package main

import (
	"context"

	"gitee.com/yscsky/yu"
	"gitee.com/yscsky/yu/app"
	"gitee.com/yscsky/yu/examples/grpc_usage/pb"
	"gitee.com/yscsky/yu/yusvr"
)

func main() {
	gs := yusvr.NewGrpcServer("GrpcServer", ":9000", setRegister, yusvr.GrpcRecovery())
	yu.Run(app.NewApp(
		"GrpcServer",
		func() bool { return true },
		func() {},
		gs,
	))
}

func setRegister(gs yusvr.IGrpcServer) {
	pb.RegisterHelloServiceServer(gs, &server{})
}

type server struct {
	pb.UnimplementedHelloServiceServer
}

func (s *server) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloResponse, error) {
	yu.Logf("%s SayHello", req.Name)
	if req.Name == "panic" {
		panic(req.Name)
	}
	return &pb.HelloResponse{Reply: "Hello " + req.Name}, nil
}
